"""
搜索和替换对话框组件
"""

from PyQt6.QtWidgets import (
    QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, 
    QPushButton, QCheckBox, QGroupBox, QTabWidget, QWidget,
    QTextEdit, QMessageBox
)
from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtGui import QFont, QKeySequence, QShortcut


class SearchDialog(QDialog):
    """搜索和替换对话框"""
    
    # 信号
    find_next = pyqtSignal(str, bool, bool)  # 搜索文本, 区分大小写, 全字匹配
    find_previous = pyqtSignal(str, bool, bool)
    find_all = pyqtSignal(str, bool, bool)
    replace = pyqtSignal(str, str, bool, bool)
    replace_all = pyqtSignal(str, str, bool, bool)
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("查找和替换")
        self.setFixedSize(500, 400)
        self.setModal(False)  # 非模态对话框
        
        self.init_ui()
        self.setup_shortcuts()
        
    def init_ui(self):
        """初始化用户界面"""
        layout = QVBoxLayout(self)
        
        # 创建标签页
        tab_widget = QTabWidget()
        tab_widget.addTab(self.create_search_tab(), "查找")
        tab_widget.addTab(self.create_replace_tab(), "替换")
        layout.addWidget(tab_widget)
        
        # 状态标签
        self.status_label = QLabel("就绪")
        self.status_label.setStyleSheet("color: #666; font-style: italic;")
        layout.addWidget(self.status_label)
        
    def create_search_tab(self):
        """创建查找标签页"""
        widget = QWidget()
        layout = QVBoxLayout(widget)
        
        # 搜索输入
        search_layout = QHBoxLayout()
        search_layout.addWidget(QLabel("查找内容:"))
        self.search_input = QLineEdit()
        self.search_input.setPlaceholderText("输入要查找的文本")
        self.search_input.textChanged.connect(self.on_search_text_changed)
        search_layout.addWidget(self.search_input)
        layout.addLayout(search_layout)
        
        # 选项组
        options_group = QGroupBox("选项")
        options_layout = QVBoxLayout(options_group)
        
        self.case_sensitive_check = QCheckBox("区分大小写")
        self.whole_word_check = QCheckBox("全字匹配")
        
        options_layout.addWidget(self.case_sensitive_check)
        options_layout.addWidget(self.whole_word_check)
        layout.addWidget(options_group)
        
        # 按钮组
        button_layout = QHBoxLayout()
        
        self.find_next_btn = QPushButton("查找下一个")
        self.find_next_btn.clicked.connect(self.find_next_clicked)
        self.find_next_btn.setDefault(True)
        
        self.find_previous_btn = QPushButton("查找上一个")
        self.find_previous_btn.clicked.connect(self.find_previous_clicked)
        
        self.find_all_btn = QPushButton("查找全部")
        self.find_all_btn.clicked.connect(self.find_all_clicked)
        
        button_layout.addWidget(self.find_next_btn)
        button_layout.addWidget(self.find_previous_btn)
        button_layout.addWidget(self.find_all_btn)
        button_layout.addStretch()
        
        layout.addLayout(button_layout)
        layout.addStretch()
        
        return widget
    
    def create_replace_tab(self):
        """创建替换标签页"""
        widget = QWidget()
        layout = QVBoxLayout(widget)
        
        # 搜索输入
        search_layout = QHBoxLayout()
        search_layout.addWidget(QLabel("查找内容:"))
        self.replace_search_input = QLineEdit()
        self.replace_search_input.setPlaceholderText("输入要查找的文本")
        search_layout.addWidget(self.replace_search_input)
        layout.addLayout(search_layout)
        
        # 替换输入
        replace_layout = QHBoxLayout()
        replace_layout.addWidget(QLabel("替换为:"))
        self.replace_input = QLineEdit()
        self.replace_input.setPlaceholderText("输入要替换的文本")
        replace_layout.addWidget(self.replace_input)
        layout.addLayout(replace_layout)
        
        # 选项组
        options_group = QGroupBox("选项")
        options_layout = QVBoxLayout(options_group)
        
        self.replace_case_check = QCheckBox("区分大小写")
        self.replace_whole_word_check = QCheckBox("全字匹配")
        
        options_layout.addWidget(self.replace_case_check)
        options_layout.addWidget(self.replace_whole_word_check)
        layout.addWidget(options_group)
        
        # 按钮组
        button_layout = QHBoxLayout()
        
        self.replace_btn = QPushButton("替换")
        self.replace_btn.clicked.connect(self.replace_clicked)
        
        self.replace_all_btn = QPushButton("全部替换")
        self.replace_all_btn.clicked.connect(self.replace_all_clicked)
        
        button_layout.addWidget(self.replace_btn)
        button_layout.addWidget(self.replace_all_btn)
        button_layout.addStretch()
        
        layout.addLayout(button_layout)
        layout.addStretch()
        
        return widget
    
    def setup_shortcuts(self):
        """设置快捷键"""
        # Ctrl+F 查找下一个
        shortcut = QShortcut(QKeySequence("Ctrl+F"), self)
        shortcut.activated.connect(self.find_next_clicked)
        
        # Ctrl+Shift+F 查找上一个
        shortcut = QShortcut(QKeySequence("Ctrl+Shift+F"), self)
        shortcut.activated.connect(self.find_previous_clicked)
        
        # Ctrl+H 替换
        shortcut = QShortcut(QKeySequence("Ctrl+H"), self)
        shortcut.activated.connect(self.replace_clicked)
        
        # Ctrl+Shift+H 全部替换
        shortcut = QShortcut(QKeySequence("Ctrl+Shift+H"), self)
        shortcut.activated.connect(self.replace_all_clicked)
        
        # Enter 查找下一个
        self.search_input.returnPressed.connect(self.find_next_clicked)
        self.replace_search_input.returnPressed.connect(self.find_next_clicked)
        
        # Escape 关闭对话框
        shortcut = QShortcut(QKeySequence("Esc"), self)
        shortcut.activated.connect(self.close)
    
    def on_search_text_changed(self, text):
        """搜索文本改变时的处理"""
        if text:
            self.find_next_btn.setEnabled(True)
            self.find_previous_btn.setEnabled(True)
            self.find_all_btn.setEnabled(True)
        else:
            self.find_next_btn.setEnabled(False)
            self.find_previous_btn.setEnabled(False)
            self.find_all_btn.setEnabled(False)
    
    def find_next_clicked(self):
        """查找下一个按钮点击"""
        search_text = self.search_input.text()
        if not search_text:
            return
            
        case_sensitive = self.case_sensitive_check.isChecked()
        whole_word = self.whole_word_check.isChecked()
        
        self.find_next.emit(search_text, case_sensitive, whole_word)
    
    def find_previous_clicked(self):
        """查找上一个按钮点击"""
        search_text = self.search_input.text()
        if not search_text:
            return
            
        case_sensitive = self.case_sensitive_check.isChecked()
        whole_word = self.whole_word_check.isChecked()
        
        self.find_previous.emit(search_text, case_sensitive, whole_word)
    
    def find_all_clicked(self):
        """查找全部按钮点击"""
        search_text = self.search_input.text()
        if not search_text:
            return
            
        case_sensitive = self.case_sensitive_check.isChecked()
        whole_word = self.whole_word_check.isChecked()
        
        self.find_all.emit(search_text, case_sensitive, whole_word)
    
    def replace_clicked(self):
        """替换按钮点击"""
        search_text = self.replace_search_input.text()
        replace_text = self.replace_input.text()
        
        if not search_text:
            return
            
        case_sensitive = self.replace_case_check.isChecked()
        whole_word = self.replace_whole_word_check.isChecked()
        
        self.replace.emit(search_text, replace_text, case_sensitive, whole_word)
    
    def replace_all_clicked(self):
        """全部替换按钮点击"""
        search_text = self.replace_search_input.text()
        replace_text = self.replace_input.text()
        
        if not search_text:
            return
            
        case_sensitive = self.replace_case_check.isChecked()
        whole_word = self.replace_whole_word_check.isChecked()
        
        self.replace_all.emit(search_text, replace_text, case_sensitive, whole_word)
    
    def set_status(self, message):
        """设置状态信息"""
        self.status_label.setText(message)
    
    def showEvent(self, event):
        """显示事件"""
        super().showEvent(event)
        self.search_input.setFocus()
        self.search_input.selectAll() 